/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2024 All Rights Reserved.
 */
package com.alipay.demo.service.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.FileItem;
import com.alipay.api.domain.*;
import com.alipay.api.internal.util.file.IOUtils;
import com.alipay.api.request.*;
import com.alipay.api.response.*;
import com.alipay.demo.service.ImageUploadService;
import com.alipay.demo.util.AlipayApiCallback;
import com.alipay.demo.util.AlipayApiTemplate;
import com.alipay.demo.util.AlipaySdkUtil;
import com.alipay.demo.vo.ImageDirectoryRequest;
import com.alipay.demo.vo.OpenResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * @author jishupei.jsp
 * @version : ImageUploadServiceImpl, v0.1 2024年03月14日 10:41 上午 jishupei.jsp Exp $
 */
@Service
public class ImageUploadServiceImpl implements ImageUploadService {
    private static final Logger logger = LogManager.getLogger(ImageUploadServiceImpl.class);

    @Autowired
    private AlipaySdkUtil alipaySdkUtil;

    @Override
    public String uploadAndReturnId(String imagePath, String uploadScene) throws AlipayApiException {
        // 构造请求参数以调用接口
        AlipayMarketingImageEnhanceUploadRequest request = new AlipayMarketingImageEnhanceUploadRequest();

        // 设置图片是否需要优化
        request.setNeedEnhance(true);

        // 设置图片上传场景
        request.setUploadScene(uploadScene);

        // 设置图片内容
        try {
            byte[] bytes = IOUtils.toByteArray(this.getClass().getResourceAsStream("/" + imagePath));
            FileItem imageContent = new FileItem(StringUtils.substringAfterLast(imagePath, "/"), bytes);
            request.setImageContent(imageContent);
        } catch (IOException e) {
            throw new AlipayApiException("alipay.marketing.image.enhance.upload调用失败:" + e);
        }

        try {
            AlipayMarketingImageEnhanceUploadResponse response = alipaySdkUtil.execute(request);
            logger.info(response.getBody());

            if (response.isSuccess()) {
                logger.info("alipay.marketing.image.enhance.upload调用成功");
                return response.getImageId();
            } else {
                logger.error("alipay.marketing.image.enhance.upload调用失败");
                throw new AlipayApiException("alipay.marketing.image.enhance.upload调用失败:" + response.getBody());
            }
        } catch (Exception e) {
            logger.error("alipay.marketing.image.enhance.upload调用失败", e);
            throw new AlipayApiException("alipay.marketing.image.enhance.upload调用失败:" + e);
        }
    }

    @Override
    public OpenResponse<String> createImageDirectory(ImageDirectoryRequest createRequest) {
        return AlipayApiTemplate.execute(new AlipayApiCallback<String, AlipayMarketingImagedirectoryCreateResponse>() {
            @Override
            public AlipayMarketingImagedirectoryCreateResponse process() throws AlipayApiException {
                // 构造请求参数以调用接口
                AlipayMarketingImagedirectoryCreateRequest request = new AlipayMarketingImagedirectoryCreateRequest();
                AlipayMarketingImagedirectoryCreateModel model = new AlipayMarketingImagedirectoryCreateModel();

                // 设置目录名
                model.setImageDirectoryName(createRequest.getImageDirectoryName());

                // 设置归属父目录id
                model.setParentDirectoryId(createRequest.getParentDirectoryId());

                request.setBizModel(model);
                return alipaySdkUtil.execute(request);
            }

            @Override
            public String getData(AlipayMarketingImagedirectoryCreateResponse response) {
                return response.getImageDirectoryId();
            }

            @Override
            public String getApiName() {
                return "alipay.marketing.imagedirectory.create";
            }
        });
    }

    @Override
    public OpenResponse<Void> updateImageDirectory(ImageDirectoryRequest updateRequest) {
        return AlipayApiTemplate.execute(new AlipayApiCallback<Void, AlipayMarketingImagedirectoryModifyResponse>() {
            @Override
            public AlipayMarketingImagedirectoryModifyResponse process() throws AlipayApiException {
                // 构造请求参数以调用接口
                AlipayMarketingImagedirectoryModifyRequest request = new AlipayMarketingImagedirectoryModifyRequest();
                AlipayMarketingImagedirectoryModifyModel model = new AlipayMarketingImagedirectoryModifyModel();

                // 设置目录id
                model.setImageDirectoryId(updateRequest.getImageDirectoryId());

                if (StringUtils.isNotBlank(updateRequest.getImageDirectoryName())) {
                    // 设置目录名
                    model.setImageDirectoryName(updateRequest.getImageDirectoryName());
                }

                if (StringUtils.isNotBlank(updateRequest.getParentDirectoryId())) {
                    // 设置父目录id
                    model.setParentDirectoryId(updateRequest.getParentDirectoryId());
                }

                request.setBizModel(model);
                return alipaySdkUtil.execute(request);
            }

            @Override
            public Void getData(AlipayMarketingImagedirectoryModifyResponse response) {
                return null;
            }

            @Override
            public String getApiName() {
                return "alipay.marketing.imagedirectory.modify";
            }
        });
    }

    @Override
    public OpenResponse<AlipayMarketingImagedirectoryListQueryResponse> queryImageDirectoryList(Long pageNum, Long pageSize, ImageDirectoryRequest queryRequest) {
        return AlipayApiTemplate.execute(new AlipayApiCallback<AlipayMarketingImagedirectoryListQueryResponse, AlipayMarketingImagedirectoryListQueryResponse>() {
            @Override
            public AlipayMarketingImagedirectoryListQueryResponse process() throws AlipayApiException {
                // 构造请求参数以调用接口
                AlipayMarketingImagedirectoryListQueryRequest request = new AlipayMarketingImagedirectoryListQueryRequest();
                AlipayMarketingImagedirectoryListQueryModel model = new AlipayMarketingImagedirectoryListQueryModel();

                // 设置页码
                model.setPageNum(pageNum == null ? 1L : pageNum);

                // 设置单页条数
                model.setPageSize(pageSize == null ? 10L : pageSize);

                if (StringUtils.isNotBlank(queryRequest.getImageDirectoryName())) {
                    // 设置目录名
                    model.setImageDirectoryName(queryRequest.getImageDirectoryName());
                }

                if (StringUtils.isNotBlank(queryRequest.getImageDirectoryId())) {
                    // 设置目录id
                    model.setImageDirectoryId(queryRequest.getImageDirectoryId());
                }

                if (StringUtils.isNotBlank(queryRequest.getParentDirectoryId())) {
                    // 设置父目录id
                    model.setParentDirectoryId(queryRequest.getParentDirectoryId());
                }

                request.setBizModel(model);
                return alipaySdkUtil.execute(request);
            }

            @Override
            public AlipayMarketingImagedirectoryListQueryResponse getData(AlipayMarketingImagedirectoryListQueryResponse response) {
                return response;
            }

            @Override
            public String getApiName() {
                return "alipay.marketing.imagedirectory.list.query";
            }
        });
    }

    @Override
    public OpenResponse<Void> updateImage(String imageIndexId, String fileName, String imageDirectoryId) {
        return AlipayApiTemplate.execute(new AlipayApiCallback<Void, AlipayMarketingImageModifyResponse>() {
            @Override
            public AlipayMarketingImageModifyResponse process() throws AlipayApiException {
                // 构造请求参数以调用接口
                AlipayMarketingImageModifyRequest request = new AlipayMarketingImageModifyRequest();
                AlipayMarketingImageModifyModel model = new AlipayMarketingImageModifyModel();

                // 设置图索引id
                model.setImageIndexId(imageIndexId);

                if (StringUtils.isNotBlank(fileName)) {
                    // 设置修改后新文件名
                    model.setFileName(fileName);
                }

                if (StringUtils.isNotBlank(imageDirectoryId)) {
                    // 设置修改后新目录id
                    model.setImageDirectoryId(imageDirectoryId);
                }

                request.setBizModel(model);
                return alipaySdkUtil.execute(request);
            }

            @Override
            public Void getData(AlipayMarketingImageModifyResponse response) {
                return null;
            }

            @Override
            public String getApiName() {
                return "alipay.marketing.image.modify";
            }
        });
    }

    @Override
    public OpenResponse<AlipayMarketingImageListQueryResponse> queryImageList(Long pageNum, Long pageSize, String imageIndexId, String fileName, String imageDirectoryId) {
        return AlipayApiTemplate.execute(new AlipayApiCallback<AlipayMarketingImageListQueryResponse, AlipayMarketingImageListQueryResponse>() {
            @Override
            public AlipayMarketingImageListQueryResponse process() throws AlipayApiException {
                // 构造请求参数以调用接口
                AlipayMarketingImageListQueryRequest request = new AlipayMarketingImageListQueryRequest();
                AlipayMarketingImageListQueryModel model = new AlipayMarketingImageListQueryModel();

                // 设置页码
                model.setPageNum(pageNum == null ? 1L : pageNum);

                // 设置单页条数
                model.setPageSize(pageSize == null ? 10L : pageSize);

                if (StringUtils.isNotBlank(imageIndexId)) {
                    // 设置图索引id
                    model.setImageIndexId(imageIndexId);
                }

                if (StringUtils.isNotBlank(fileName)) {
                    // 设置图文件名
                    model.setFileName(fileName);
                }

                if (StringUtils.isNotBlank(imageDirectoryId)) {
                    // 设置图归属目录id
                    model.setImageDirectoryId(imageDirectoryId);
                }

                request.setBizModel(model);
                return alipaySdkUtil.execute(request);
            }

            @Override
            public AlipayMarketingImageListQueryResponse getData(AlipayMarketingImageListQueryResponse response) {
                return response;
            }

            @Override
            public String getApiName() {
                return "alipay.marketing.image.list.query";
            }
        });
    }

    @Override
    public OpenResponse<Void> deleteImage(List<String> imageIndexIdList) {
        return AlipayApiTemplate.execute(new AlipayApiCallback<Void, AlipayMarketingImageDeleteResponse>() {
            @Override
            public AlipayMarketingImageDeleteResponse process() throws AlipayApiException {
                // 构造请求参数以调用接口
                AlipayMarketingImageDeleteRequest request = new AlipayMarketingImageDeleteRequest();
                AlipayMarketingImageDeleteModel model = new AlipayMarketingImageDeleteModel();

                model.setImageIndexIdList(imageIndexIdList);

                request.setBizModel(model);
                return alipaySdkUtil.execute(request);
            }

            @Override
            public Void getData(AlipayMarketingImageDeleteResponse response) {
                return null;
            }

            @Override
            public String getApiName() {
                return "alipay.marketing.image.delete";
            }
        });
    }
}
